﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Windows;
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Ribbon;
using DevExpress.Xpf.RichEdit;
using ICSharpCode.Core;
using Caliburn.Micro;

namespace Agnes.Core
{
    /// <summary>
    /// Agnes 工作区服务
    /// 通过AgnesServiceManager获取该实例
    /// </summary>
    public class AgnesWorkBenchService : PropertyChangedBase
    {
        /// <summary>
        /// DX 的 BarItem
        /// </summary>
        public ObservableCollection<BarItem> BarItems { get; private set; }

        /// <summary>
        /// DX 的 RibbonPages
        /// </summary>
        public ObservableCollection<RibbonPage> RibbonPages { get; private set; }

        /// <summary>
        /// DX 的 文档集合
        /// </summary>
        public ObservableCollection<IDocumentPanel> DocumentPanels { get; set; }

        /// <summary>
        /// DX 的 左面板， 主要用于各类信息概览或者解决方案
        /// </summary>
        public ObservableCollection<IWorkbenchPad> LeftPads { get; private set; }

        /// <summary>
        /// DX 的右面板， 主要用于各种工具，会自动影藏
        /// </summary>
        public ObservableCollection<IWorkbenchPad> RightPads { get; private set; }

        /// <summary>
        /// DX 的 底部面板， 主要用于信息输出
        /// </summary>
        public ObservableCollection<IWorkbenchPad> BottomPads { get; private set; }

        private RichEditControl _focusedRichEditControl = null;
        /// <summary>
        /// 获得焦点的插件
        /// </summary>
        public RichEditControl FocusedRichEditControl
        {
            get
            {
                return _focusedRichEditControl;
            }
            set 
            {
                _focusedRichEditControl = value; 
                NotifyOfPropertyChange(() => FocusedRichEditControl);
            }
        }

        private IDocumentPanel _focusedDocumentPanel = null;
        /// <summary>
        /// 被关注的文档
        /// </summary>
        public IDocumentPanel FocusedDocumentPanel
        {
            get { return _focusedDocumentPanel; }
            set
            {
                _focusedDocumentPanel = value;
                NotifyOfPropertyChange(() => FocusedDocumentPanel);
            }
        }

        internal AgnesWorkBenchService()
        {
            DocumentPanels = new ObservableCollection<IDocumentPanel>();
            BarItems = new ObservableCollection<BarItem>();
            RibbonPages = new ObservableCollection<RibbonPage>();
            LeftPads = new ObservableCollection<IWorkbenchPad>();
            RightPads = new ObservableCollection<IWorkbenchPad>();
            BottomPads = new ObservableCollection<IWorkbenchPad>();
            LoadWorkBenchPads();
            LoadRibbonParts();
            AgnesServiceManager.AgnesSlnService.SlnClosedHandler += new EventHandler(AgnesSlnService_SlnClosedHandler);
        }

        void AgnesSlnService_SlnClosedHandler(object sender, EventArgs e)
        {
            for (int i = 0; i < DocumentPanels.Count; i++)
            {
                DocumentPanels.RemoveAt(i);
            }
            FocusedDocumentPanel = null;
            FocusedRichEditControl = null;
        }

        /// <summary>
        /// 加载工作区面板
        /// </summary>
        public void LoadWorkBenchPads()
        {
            try
            {
                AgnesServiceManager.LogServ.Info("加载工作区面板");
                foreach (WorkbenchPadDiscriptor i in AddInTree.BuildItems<WorkbenchPadDiscriptor>("Agnes/Workbench/WorkbenchPad", null))
                {
                    IWorkbenchPad workbenchPad = (i.Codon.AddIn.CreateObject(i.Codon.Properties["class"]) as IWorkbenchPad);

                    AgnesServiceManager.AgnesSlnService.SlnOpenedHanlder += workbenchPad.SlnOpened;
                    AgnesServiceManager.AgnesSlnService.SlnClosedHandler += workbenchPad.SlnClosed;
                    
                    switch (workbenchPad.WorkbenchPadPosition)
                    {
                        case WorkbenchPadPosition.Right:
                            RightPads.Add(workbenchPad);
                            break;
                        case WorkbenchPadPosition.Left:
                            LeftPads.Add(workbenchPad);
                            break;
                        case WorkbenchPadPosition.Bottom:
                            BottomPads.Add(workbenchPad);
                            break;
                    }
                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        /// <summary>
        /// 加载BarItems
        /// </summary>
        public void LoadRibbonParts()
        {
            try
            {
                List<ToolPadDiscriptor> toolPads = AddInTree.BuildItems<ToolPadDiscriptor>("Agnes/Mainframe/ToolPad", null);
                foreach (ToolPadDiscriptor i in toolPads)
                {
                    ResourceDictionary rd = new ResourceDictionary();
                    string path = i.Codon.AddIn.FileName.Replace(".addin", ".dll");
                    Assembly.LoadFrom(path);
                    rd.Source = new Uri("pack://application:,,,/" + i.Codon.AddIn.Name + ";Component/"
                                        + i.Codon.Properties["PadStyle"], UriKind.RelativeOrAbsolute);

                    //加载BarItem
                    BarItemCollection barItemCollection = rd[i.Codon.Properties["BarItemsKey"]] as BarItemCollection;
                    if (barItemCollection != null)
                    {
                        foreach (BarItem barItem in barItemCollection)
                        {
                            BarItems.Add(barItem);
                        }
                    }

                    //加载RibbonPage
                    RibbonPages.Add(rd[i.Codon.Properties["RibbonPageKey"]] as RibbonPage);
                }
            }
            catch (Exception ex)
            { 
            
            }
        }
    }
}
